import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './indx.less';
import {Toast, List, InputItem, Button, Modal, WingBlank, WhiteSpace, Switch, Card} from 'antd-mobile';
import {routerRedux} from 'dva/router';
import Block from 'fs-flex'
import Header from '../../../../../../components/Header/index'
import SysBusiTypeService from '../../../../../../services/sysBusiTypeService'
import BusiTypeService from '../../../../../../services/busiTypeService'
import CmsService from '../../../../../../services/CmsService'
import Constant from '../../../../../../utils/constant';
import Select from '../../../../../../components/Select/index'



const alert = Modal.alert
class SettingPage extends React.Component{
  state={
      show:false,
      busiTypeArticles:[]
  }

  async componentDidMount(){
      const {code,data}=await CmsService.list();
      if (Constant.responseOK === code) {
        this.setState({ busiTypeArticles: data })
    }

  }

  async save(){
      const {form} =this.props;
      const {busitypename,busitypecount,timecount1,timecount2,timecount3,timecount4,timecount5,timecount6,timecount7,
      Switch1,Switch2,Switch3,Switch4,Switch5,Switch6,Switch7,time1,time2,time3,time4,time5,time6,time7} = form.getFieldsValue()

      if((!timecount1&&Switch1)||(!timecount2&&Switch2)||(!timecount3&&Switch3)||(!timecount4&&Switch4)||(!timecount5&&Switch5)||(!timecount6&&Switch6)||(!timecount7&&Switch7)){
          Toast.fail('请填写时段业务上限！',2)
          return
      }
      // if(!timecount1||!timecount2||!timecount3||!timecount4||!timecount5||!timecount6||!timecount7){
      //     Toast.fail('请填写时段业务上限！',2)
      //     return
      // }
      if(!busitypename){
        Toast.fail('请填写业务类型名称！',2)
        return
      }
      if(!busitypecount){
        Toast.fail('请填写业务类型名称！',2)
        return
      }
      let id1 = 0;
      let id2 = 0;
      let id3 = 0;
      let id4 = 0;
      let id5 = 0;
      let id6 = 0;
      let id7 = 0;
      for(let i=0;i<this.props.location.state.data.length;i++){
          if(this.props.location.state.data[i].time_start==time1.split("-")[0]){
              id1 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time2.split("-")[0]){
              id2 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time3.split("-")[0]){
              id3 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time4.split("-")[0]){
              id4 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time5.split("-")[0]){
              id5 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time6.split("-")[0]){
              id6 = this.props.location.state.data[i].time_id;
          }
          if(this.props.location.state.data[i].time_start==time7.split("-")[0]){
              id7 = this.props.location.state.data[i].time_id;
          }
      }

      let params = [
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time1.split("-")[0],
              time_end:time1.split("-")[1],
              time_count:timecount1,
              time_id:id1,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch1
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time2.split("-")[0],
              time_end:time2.split("-")[1],
              time_count:timecount2,
              time_id:id2,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch2
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time3.split("-")[0],
              time_end:time3.split("-")[1],
              time_count:timecount3,
              time_id:id3,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch3
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time4.split("-")[0],
              time_end:time4.split("-")[1],
              time_count:timecount4,
              time_id:id4,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch4
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time5.split("-")[0],
              time_end:time5.split("-")[1],
              time_count:timecount5,
              time_id:id5,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch5
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time6.split("-")[0],
              time_end:time6.split("-")[1],
              time_count:timecount6,
              time_id:id6,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch6
          },
          {
              busitypename:busitypename,
              busitype_count:busitypecount,
              time_start:time7.split("-")[0],
              time_end:time7.split("-")[1],
              time_count:timecount7,
              time_id:id7,
              busitype_id:this.props.location.state.data[0].busitype_id,
              time_enable:Switch7
          },
      ]
      console.log(params)
      const {code,msg} = await SysBusiTypeService.updateBusiType(params)
      console.log(code)
      if(code==="0000"){
          Toast.info("更新成功！",2)
          setTimeout(() => {
              this.props.history.push('/manage/sysBusiType')
          }, 2000);
      }
  }

  render(){
      const { getFieldProps  } = this.props.form;
      return(
          <Block vf  className={styles.busiContent}>
              <Block>
                  <Header title='业务类型编辑' needBack="true" history={this.props.history}/>
              </Block>
              <WingBlank size="lg">
                  <WhiteSpace size="lg" />
                      <List>
                          <List.Item>
                              <InputItem
                                    {...getFieldProps('busitypename', { initialValue:this.props.location.state && this.props.location.state.data?this.props.location.state.data[0].busitypename:""  })}
                                    clear
                                    placeholder="请输入业务业务名称"
                                    ref={el => this.autoFocusInst = el}>
                              </InputItem>
                              <Block className={styles.ipt1}>
                                  <InputItem
                                      {...getFieldProps('busitypecount', { initialValue:this.props.location.state && this.props.location.state.data?this.props.location.state.data[0].busitype_count:"0" })}
                                      clear
                                      type="number"
                                      placeholder="请输入业务每日上限数，只能为整数"
                                      ref={el => this.autoFocusInst = el}>每日临时排号上限
                                  </InputItem>
                              </Block>

                              <Block wf>
                                  <Block w={140} className={styles.ipt2}>
                                      <InputItem value="时段"  editable={false}></InputItem>
                                  </Block>
                                  <Block w={70}>
                                      <InputItem value="号数"  editable={false}></InputItem>
                                  </Block>
                                  <Block f='1' className={styles.ipt4}>
                                      <InputItem value="启用状态"  editable={false}></InputItem>
                                  </Block>
                              </Block>

                              <Block wf>
                                  <Block w={140} className={styles.ipt2}>
                                      <InputItem editable={false}
                                        {...getFieldProps('time1', { initialValue:this.props.location.state.data[0].time_start+'\-'+this.props.location.state.data[0].time_end })}
                                        clear
                                        ref={el => this.autoFocusInst = el}>
                                        </InputItem>
                                  </Block>
                                  <Block w={60}>
                                      <InputItem
                                          {...getFieldProps('timecount1', { initialValue:this.props.location.state.data[0].time_count })}
                                          clear
                                          type="number"
                                          ref={el => this.autoFocusInst = el}>
                                          </InputItem>
                                  </Block>
                                  <Block f='1' className={styles.ipt4}>
                                      <List.Item
                                      extra={<Switch
                                      {...getFieldProps('Switch1', {
                                      initialValue: this.props.location.state.data[0].time_enable,
                                      valuePropName: 'checked',
                                      })}
                                      />}
                                      ></List.Item>
                                  </Block>
                              </Block>

                              <Block wf>
                                  <Block w={140} className={styles.ipt2}>
                                      <InputItem editable={false}
                                          {...getFieldProps('time2', { initialValue:this.props.location.state.data[1].time_start+'\-'+this.props.location.state.data[1].time_end })}
                                          clear
                                          ref={el => this.autoFocusInst = el}>
                                      </InputItem>
                                  </Block>
                                  <Block w={60}>
                                      <InputItem
                                          {...getFieldProps('timecount2', { initialValue:this.props.location.state.data[1].time_count })}
                                          clear
                                          type="number"
                                          ref={el => this.autoFocusInst = el}>
                                      </InputItem>
                                  </Block>
                                  <Block f='1' className={styles.ipt4}>
                                      <List.Item
                                          extra={<Switch
                                          {...getFieldProps('Switch2', {
                                            initialValue: this.props.location.state.data[1].time_enable,
                                            valuePropName: 'checked',
                                          })}
                                          />}
                                      ></List.Item>
                                  </Block>
                              </Block>

                              <Block wf>
                                  <Block w={140} className={styles.ipt2}>
                                      <InputItem editable={false}
                                        {...getFieldProps('time3', { initialValue:this.props.location.state.data[2].time_start+'\-'+this.props.location.state.data[2].time_end })}
                                        clear
                                        ref={el => this.autoFocusInst = el}>
                                      </InputItem>
                                  </Block>
                                  <Block w={60}>
                                    <InputItem
                                  {...getFieldProps('timecount3', { initialValue:this.props.location.state.data[2].time_count })}
                                  clear
                                  type="number"
                                  ref={el => this.autoFocusInst = el}>
                                </InputItem>
                                </Block>
                                <Block f='1' className={styles.ipt4}>
                                    <List.Item
                                      extra={<Switch
                                      {...getFieldProps('Switch3', {
                                        initialValue: this.props.location.state.data[2].time_enable,
                                        valuePropName: 'checked',
                                      })}
                                      />}
                                      ></List.Item>
                                </Block>
                          </Block>

                          <Block wf>
                                <Block w={140} className={styles.ipt2}>
                              <InputItem editable={false}
                                {...getFieldProps('time4', { initialValue:this.props.location.state.data[3].time_start+'\-'+this.props.location.state.data[3].time_end })}
                                clear
                                ref={el => this.autoFocusInst = el}>
                              </InputItem>
                                </Block>
                                <Block w={60}>
                                  <InputItem
                                {...getFieldProps('timecount4', { initialValue:this.props.location.state.data[3].time_count })}
                                clear
                                type="number"
                                ref={el => this.autoFocusInst = el}>
                              </InputItem>
                                </Block>
                                <Block f='1' className={styles.ipt4}>
                              <List.Item
                                extra={<Switch
                                {...getFieldProps('Switch4', {
                                  initialValue: this.props.location.state.data[3].time_enable,
                                  valuePropName: 'checked',
                                })}
                                />}
                                ></List.Item>
                                </Block>
                          </Block>

                          <Block wf>
                              <Block w={140} className={styles.ipt2}>
                            <InputItem editable={false}
                              {...getFieldProps('time5', { initialValue:this.props.location.state.data[4].time_start+'\-'+this.props.location.state.data[4].time_end })}
                              clear
                              ref={el => this.autoFocusInst = el}>
                            </InputItem>
                              </Block>
                              <Block w={60}>
                                <InputItem
                              {...getFieldProps('timecount5', { initialValue:this.props.location.state.data[4].time_count })}
                              clear
                              type="number"
                              ref={el => this.autoFocusInst = el}>
                            </InputItem>
                              </Block>
                              <Block f='1' className={styles.ipt4}>
                            <List.Item
                              extra={<Switch
                              {...getFieldProps('Switch5', {
                                initialValue: this.props.location.state.data[4].time_enable,
                                valuePropName: 'checked',
                              })}
                              />}
                              ></List.Item>
                              </Block>
                          </Block>

                          <Block wf>
                              <Block w={140} className={styles.ipt2}>
                            <InputItem editable={false}
                              {...getFieldProps('time6', { initialValue:this.props.location.state.data[5].time_start+'\-'+this.props.location.state.data[5].time_end })}
                              clear
                              ref={el => this.autoFocusInst = el}>
                            </InputItem>
                              </Block>
                              <Block w={60}>
                                <InputItem
                              {...getFieldProps('timecount6', { initialValue:this.props.location.state.data[5].time_count })}
                              clear
                              type="number"
                              ref={el => this.autoFocusInst = el}>
                            </InputItem>
                              </Block>
                              <Block f='1' className={styles.ipt4}>
                            <List.Item
                              extra={<Switch
                              {...getFieldProps('Switch6', {
                                initialValue: this.props.location.state.data[5].time_enable,
                                valuePropName: 'checked',
                              })}
                              />}
                              ></List.Item>
                              </Block>
                          </Block>

                          <Block wf>
                                <Block w={140} className={styles.ipt2}>
                              <InputItem editable={false}
                                {...getFieldProps('time7', { initialValue:this.props.location.state.data[6].time_start+'\-'+this.props.location.state.data[6].time_end })}
                                clear
                                ref={el => this.autoFocusInst = el}>
                              </InputItem>
                                </Block>
                                <Block w={60}>
                                  <InputItem
                                {...getFieldProps('timecount7', { initialValue:this.props.location.state.data[6].time_count })}
                                clear
                                type="number"
                                ref={el => this.autoFocusInst = el}>
                              </InputItem>
                                </Block>
                                <Block f='1' className={styles.ipt4}>
                              <List.Item
                                extra={<Switch
                                {...getFieldProps('Switch7', {
                                  initialValue: this.props.location.state.data[6].time_enable,
                                  valuePropName: 'checked',
                                })}
                                />}
                                ></List.Item>
                                </Block>
                              </Block>
                          </List.Item>
                      </List>

                  <WhiteSpace size="lg" />
                  <Block wf mt={15}>
                      <Block f='3'></Block>
                      <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={() =>
                          this.save()}
                      >保存</Button></Block>
                      <Block f='3'></Block>
                  </Block>
              </WingBlank>
          </Block>

      );
  }
}

SettingPage
  .propTypes = {
};
export default createForm()(connect()(SettingPage));
